<?php

use yii\helpers\Html;
use backend\modules\supplier\models\GoodsClass;
use yii\bootstrap\Modal;
use yii\widgets\ActiveForm;


/* @var $this yii\web\View */
/* @var $searchModel backend\modules\supplier\models\GoodsClassSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '商品分类';
$this->params['breadcrumbs'][] = $this->title;

$tree = GoodsClass::getTree();
?>
<style>
    ul{padding-left:0px;}
    .list li{padding:0px;list-style:none;clear:both;overflow:auto;padding-left:30px;}
    .list li .item{margin:8px auto;}
    .list li .name{float:left;}
    .title{background:#ddd;font-size:14px;font-weight:bold;padding-top:8px;padding-bottom:8px;}
    .title .name{margin-left: 18px;}
    .sort,.option{float:right;margin-right:18px;display:block;width:80px;text-align:center;}
</style>

<div class="goods-class-index">

    <h1><?= Html::encode($this->title) ?></h1>
    
    <p>        
        <?php
        $form = ActiveForm::begin([
            'id'=>'create-form',
            'action' => ['create'],
            'enableAjaxValidation' => false,
            'enableClientValidation' => true,
            'options'=>['onsubmit'=>'return false;'],
        ]);

        Modal::begin([
            'id' => 'createDialog',
            'header' => '<b style="font-size:16px;">新建商品分类</b>',
            'toggleButton' => ['label' => '新建根分类', 'class' => 'btn btn-success','id'=>'btn-create'],
            'footer' => Html::submitButton('新建', ['class' => 'btn btn-primary','onclick'=>'return createSubmit(this);']) . 
                        Html::button('取消', ['class' => 'btn btn-danger', 'onclick' => 'closeDialog();']),
        ]);

        $model = new GoodsClass;
        $model->parent_id = 0;

        echo $form->field($model, 'parent_id')->label(false)->hiddenInput();
        echo $form->field($model, 'name')->textInput(['maxlength' => 45]);
        echo $form->field($model, 'sort')->textInput();


        Modal::end();
        ActiveForm::end();
        ?>
    </p>
    
    <div class="title">
        <span class="name">名称</span>
        <span class="option">操作</span>
        <span class="sort">排序</span>
    </div>
    
    <div id="goods-class-tree">
        <?=
        $this->render('_sub', [
            'tree' => $tree,
            'is_first'=>1,
        ])
        ?>
    </div>
</div>

<script>
    window.onload=function(){
        input_onchange();
    };
    
    function del(id) {
        var msg = "删除该分类后，该分类下的商品将重置为未分类。\n\n您确认吗？";
        if (confirm(msg) == true) {
            var url = '<?= \yii\helpers\Url::to(['delete']); ?>';
            $.ajax({url: url, type: 'POST', dataType: 'json', data: {id: id}, success: function (ret) {
                    if(ret.result==false){
                        alert(ret.content.error_msg);
                    }else{
                        $('#goods-class-tree').html(ret.content);
                    }
                }});
        }
    }
    
    function input_onchange(e){
        $('.input').change(function(){
            var val=$(this).val();
            var id=$(this).attr('data-id');
            var type=$(this).attr('data-type');
            
            $.ajax({
                url:'<?= \yii\helpers\Url::to(['update']); ?>',
                type: 'POST',
                dataType:'json',
                data: {val:val,id:id,type:type},
                success: function (ret) {
                      if(ret.result==true){
                          $('#goods-class-tree').html(ret.content);
                      }else{
                          alert(ret.error_msg);
                      }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                      alert(textStatus);
                }
            });
        });
        //$('#goods-class-tree ')
    }
   
   function create(pid){
       $('#goodsclass-parent_id').val(pid);
       $('#goodsclass-name').val('');
       $('#goodsclass-sort').val('');
       $('#btn-create').click();
   }
   
   function closeDialog(){
        $("#createDialog .modal-header .close").click();
    }
   
    function createSubmit(e){
        $(e).html('数据提交中').attr('disabled',true);
        
        $.ajax({
            url:'<?= \yii\helpers\Url::to(['create']); ?>',
            type: 'POST',
            dataType:'json',
            data: $('#create-form').serializeArray(),
            success: function (ret) {
                  if(ret.result==true){
                      $('#goods-class-tree').html(ret.content);
                      closeDialog();
                  }else{
                      alert(ret.error_msg);
                  }
                  $(e).html('新建').attr('disabled',false);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus);
                  $(e).html('新建').attr('disabled',false);
            }
        });
        return false;
    }
</script>
